package cc.rengu.igas.momp.common.util;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author Jinan Liu
 * @date 2020/4/14 14:01
 * @description 日期-周计算工具方法
 */

public class WeekFormat {

    /**
     * 计算当前日期是一周里的第几天
     * <p>
     * Monday - 1
     * Tuesday - 2
     * Wednesday - 3
     * Thursday - 4
     * Friday - 5
     * Saturday - 6
     * Sunday - 7
     *
     * @param sdate
     * @return int
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/14 14:01
     */
    public int getWeek(String sdate) {

        Date date = strToDate(sdate);
        Calendar c = Calendar.getInstance();
        c.setTime(date);

        int week = c.get(Calendar.DAY_OF_WEEK);
        /* hour中存的就是星期几了,其范围 1~7,1=星期日 7=星期六，其他类推 */
        if (1 == week) {
            return 7;
        } else {
            return week - 1;
        }
    }

    /**
     * 时间日期转换
     *
     * @param strDate
     * @return java.util.Date
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/14 14:01
     */
    public Date strToDate(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }
}
